package com.neuedu.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class MQMsgController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //可靠同步
    @RequestMapping("/order/sync/{msg}")
    public String syncMsg(@PathVariable("msg") String msg) {
        for (int i = 0; i < 100; i++) {
            rocketMQTemplate.syncSend("sync-msg", "No" + i + ": " + msg);
            log.info("发送消息，消息内容为：{}", "No" + i + ": " + msg);
        }
        return "发送的消息是：" + msg;
    }
    //可靠异步
    @RequestMapping("/order/async/{msg}")
    public String asyncSend(@PathVariable("msg") String msg) {
        for (int i = 0; i < 100; i++) {
            rocketMQTemplate.asyncSend("async-msg", "No" + i + ": " + msg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("消息发送成功，结果为{}", sendResult);
                }
                @Override
                public void onException(Throwable throwable) {
                    log.error("消息发送失败，异常为", throwable);
                }
            });
        }
        return "发送的消息是：" + msg;
    }
    //单向发送
    @RequestMapping("/order/oneway/{msg}")
    public String oneWayMsg(@PathVariable("msg") String msg){
        for (int i = 0; i < 100; i++) {
            rocketMQTemplate.sendOneWay("oneway-msg", "No" + i + ": " + msg);
            log.info("发送消息，消息内容为：{}", "No" + i + ": " + msg);
        }
        return "发送的消息是：" + msg;
    }
    //同步顺序消息
    @RequestMapping("/order/orderly/{msg}")
    public String orderlyMsg(@PathVariable("msg") String msg){
        for (int i = 0; i < 100; i++) {
            rocketMQTemplate.syncSendOrderly("orderly-msg", "No" + i + ": " + msg, "orderly-msg-queue");
            log.info("发送消息，消息内容为：{}", "No" + i + ": " + msg);
        }
        return "发送的消息是：" + msg;
    }




}

